//
//  DiscoverExploreListCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/19.
//

#import "DiscoverExploreListCell.h"

@interface DiscoverExploreListCell()

@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *contentLabel;
@property(nonatomic,strong)UIView *line;

@end

@implementation DiscoverExploreListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    __weak typeof(self)self_weak_ = self;
    self.articleImageView = [[UIImageView alloc]init];
    self.articleImageView.backgroundColor = KbgColor;
    self.articleImageView.layer.cornerRadius = 6;
    self.articleImageView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.articleImageView];
    [self.articleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(@68);
        make.centerY.mas_equalTo(self_weak_.contentView);
        make.right.mas_equalTo(@-16);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.font = JPUIFontPingFangSCMedium(16);
    self.titleLabel.textColor = KFontBlackColor;
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@27);
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(self_weak_.articleImageView.mas_left).offset(-20);
        make.height.mas_equalTo(@22);
    }];
    
    self.contentLabel = [[UILabel alloc]init];
    self.contentLabel.font = JPUIFontPingFangSCRegular(14);
    self.contentLabel.textColor = KFontGrayColor;
    self.contentLabel.numberOfLines = 2;
    [self.contentView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(self_weak_.articleImageView.mas_left).offset(-20);
        make.top.mas_equalTo(self_weak_.titleLabel.mas_bottom).offset(6);
    }];
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = KLineColor;
    [self.contentView addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@16);
        make.right.mas_equalTo(@-16);
        make.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
    }];
    
    
}


-(void)setModel:(DiscoverExploreListModel *)model{
    _model = model;
    
    self.titleLabel.text = model.name;
    self.contentLabel.text = model.content;
    [self.articleImageView sd_setImageWithURL:[NSURL URLWithString:model.imagePath]];
    
    CGFloat h0 = [self getStringHeightWithText:@"日语岛" font:self.contentLabel.font viewWidth:kScreenWidth-32-68-20];
    CGFloat h = [self getStringHeightWithText:model.content font:self.contentLabel.font viewWidth:kScreenWidth-32-68-20];
    if(h > h0){
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(@17);
        }];
    }else{
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(@27);
        }];
    }
}

@end
